home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2887 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.7 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Members: Difficult Question
  5. Date: 20 Jan 1996 00:11:14 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4dpbv2$l6k@colossus.holonet.net>
  8. References: <4dmha6$80c@marlin.ssnet.com>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Ray Helie (helie@ssnet.com) wrote:
  13. [...]
  14. : In the microsoft foundation class library, there is a CObject class.  I
  15. : create an inherited class that is a virtual class as follows:
  16.                                       ^^^^^^^ actually, "abstract"
  17. [...]
  18. : class C_BASE : public CObject
  19. [...]
  20. : class C_ITEM : public C_BASE
  21. [...]
  22. : void func1 (CObject* pItem)
  23. :     {
  24. :     C_BASE* base;
  25. :     base = pItem;
  26.         ????????????  ILLEGAL
  27. You should get a compile error on this stmt.  No automatic
  28. conversion exists from a base pointer to a derived pointer.
  29.  
  30. :     pItem-> index (); 
  31. :     // (i) is the above call to index () allowed?  or did i lose virtual 
  32. :     // information about the class C_BASE when I went to the CObject
  33. :     // pointer?
  34.  
  35. It should be illegal, with a compile error.  CObject has no
  36. index member.  It *would* be legal to have base->index(),
  37. assuming you did the cast right on the previous statement.
  38. Not terribly safe, but legal.
  39.     
  40.  
  41. : On to the difficult part:
  42. [...]
  43. : I'm reading and writing object states to disk...
  44.  
  45. Oh boy, I smell trouble.  Are you expecting to use objects
  46. saved from a previous run, for example?  The vtbl pointers
  47. (and any other pointers) will be all wrong.  This is the
  48. classic problem of "persistent objects" which requires a
  49. very careful solution.
  50.  
  51. : main ()
  52. :     {
  53. :     C_ITEM item;
  54. :     writeObject ((CObject*)&item,sizeof(C_ITEM));
  55. :     readObject  ((CObject*)&item,sizeof(C_ITEM));
  56. :     }
  57.  
  58.  
  59. : void readObject (CObject* pObject,int pSize)
  60. :     {
  61. :     C_BASE* base = pObject;
  62.                      ^^^^^^^^^^ ??? NEEDS CAST, SEE ABOVE
  63. :     base-> index (); // this call works fine
  64. :     Read ((char*)&pObject,pSize); 
  65.  
  66. A few things are very wrong here.  The "&" is surely wrong, as
  67. Read apparently is going to store pSize bytes at the location of
  68. this pointer, but its size is just sizeof(CObject*), i.e. 2 or
  69. 4 bytes.
  70.  
  71. If you remove the "&", it has at least a chance of working, but
  72. the C++ standard gives you no guarantee that (CObject*)&item
  73. has the same numerical value as &item.  I think that it actually
  74. will not in some popular C++ implementations (owing to the vtbl),
  75. but maybe you are OK in VC++.
  76.  
  77. More important, perhaps, are the caution flags I raised earlier.
  78. It just is not a good idea to treat an object like a bucket of
  79. bits, as your (char*) cast implies you're about to do.
  80.  
  81. Good luck.
  82. --
  83. Russell Blackadar    russell@mdli.com
  84.